using System;
using System.Collections.Generic;
using System.Text;

public class TokenString
{
    private string[] strElements;

    // Tokenize the passed string.
    public TokenString(string strInput, char seperator)
    {
        int iTokenCount = 0;

        // Count the elements in the string
        for (int i = 0; i < strInput.Length; i++)
        {
            if (strInput[i] == '"')
                for (i++; (i < strInput.Length) && (strInput[i] != '"'); i++) ;
            if (i >= strInput.Length)
                throw new Exception("Unclosed quote in line");
            if (strInput[i] == seperator)
                iTokenCount++;
        }
        if (strInput.Length > 0)
            iTokenCount++;

        // Create a new array to hold the tokens
        strElements = new string[iTokenCount];
        for (int i = 0; i < iTokenCount; i++)
            strElements[i] = "";
        iTokenCount = 0;

        for (int i = 0; i < strInput.Length; i++)
        {
            if (strInput[i] == '"')
            {
                for (i++; (i < strInput.Length) && (strInput[i] != '"'); i++)
                    strElements[iTokenCount] += strInput[i];
            }
            else
            {
                if (strInput[i] == seperator)
                {
                    strElements[iTokenCount] = strElements[iTokenCount].Trim();
                    iTokenCount++;
                }
                else
                    strElements[iTokenCount] += strInput[i];
            }
        }
        strElements[iTokenCount] = strElements[iTokenCount].Trim();
    }

    public int TokenCount
    {
        get { return strElements.Length; }
    }

    public string this[int iToken]
    {
        get
        {
            return strElements[iToken];
        }
    }
}
